//
//  XTStatusRetweetView.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/15.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
import SnapKit

class XTStatusRetweetView: UIView {

    var retweetViewBottom: Constraint?
    //加载数据
    var statusViewModel:XTStatusViewModel?{
        didSet{
        contentLabel.text = statusViewModel?.retweetContent
            
            //卸载约束
            self.retweetViewBottom?.uninstall()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0{
            //显示配图
            picView.hidden = false
                //赋值
                picView.picUrls = picUrls
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(picView).offset(margin).constraint
                })
            }else{
            //隐藏配图
                picView.hidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(margin).constraint
                })
            }
        }
    }
    
    
        //  MARK: -懒加载控件-
    private lazy var contentLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fronSize: 14)
        label.numberOfLines = 0
        
        return label
        
    }()
    
    private lazy var picView:XTStatusPicView = {
    let view = XTStatusPicView()
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
    
        addSubview(contentLabel)
        addSubview(picView)
        
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(margin)
            make.top.equalTo(self).offset(margin)
            make.trailing.equalTo(self).offset(-margin)
        }
        
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.leading.equalTo(contentLabel)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //当前整个视图的约束
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottom = make.bottom.equalTo(picView).offset(margin).constraint
        }
    }

}


